#ifndef SEL_H_INCLUDED
#define SEL_H_INCLUDED

#include "act_types.h"
#include "mhdef.h"
#include "ccycle.h"
#include "feedback.h"
#include "timer.h"

typedef struct _SEL SEL;

struct _SEL
{
    uchar state[16];        /* 0-15:q1, q2, h1, h2, q3, q4, h3, h4, q5, q6, h5, h6, q7, q8, h7, h8*/
    uchar group_state[16];

    uchar state_bk[16];     /* 备份状态 */
    CCYCLE cycle;
    CCYCLE reset_cycle;
    CHECK_TIMER timer;
    uchar all_cycle;
    uchar resetall;

    int (*exec)(MH_UDP *pudp, int index, uchar *state);
    int (*reset)(MH_UDP *pudp, int group, int index);
    int (*reset_all)(MH_UDP *pudp, int index, int type);
};


SEL *sel_new();
void sel_set_cycle(SEL *sel, CCYCLE *cycle, CCYCLE *reset_cycle);
int sel_exec(MH_UDP *pudp, SEL *sel, ActType type, int group, int num);
int sel_reset_all(MH_UDP *pudp, SEL *sel, int ctype, int index);
int sel_exec_all(MH_UDP *pudp, SEL *sel, ActType type, int ctype, int index);
int sel_exec_cycle(MH_UDP *pudp, void *user_data);
int sel_restore(MH_UDP *pudp, void *user_data);
void sel_start_timer(SEL *sel, TIMEOUT_FUNC pfunc, BOOL now);
int sel_exec_timer(MH_UDP *pudp, SEL *sel);
void sel_stop_cycle(SEL *sel);
int sel_check_error(MH_UDP *pudp, void *user_data);

BOOL sel_reset_complete(SEL *sel);

void sel_set_state(SEL *sel, uchar *state, int size);
void sel_get_state(SEL *yc, uchar *state, int size);
int sel_encount_error(SEL *sel, FEEDBACK *fb);

void sel_set_all_cycle(SEL *sel, uchar all_cycle);
void sel_set_reset_all(SEL *sel, uchar resetall);

CCYCLE* sel_get_cycle(SEL *sel);
BOOL sel_need_restore(SEL *sel);

void sel_set_group_state(SEL *sel, uchar group_state, int group);
uchar sel_get_group_state(SEL *sel, int group);
int sel_exec_group(MH_UDP *pudp, SEL *sel, int group);

#endif // SEL_H_INCLUDED
